/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author alan
 */
public class ThreadMBeanMemoria implements Runnable {

    private MBeanServerConnection JMXSC;
    private JTextField jtfMemOcupada;
    private JTextField jtfMemLivre;
    Long memLivre = null;
    Long memOcupada = null;
    ObjectName controle;
    JTextArea jtaMemoria;

    public ThreadMBeanMemoria(ObjectName controle, MBeanServerConnection JMXSC, JTextField jtfMemOcupada, JTextField jtfMemLivre) {
        this.jtfMemLivre = jtfMemLivre;
        this.jtfMemOcupada = jtfMemOcupada;

        this.JMXSC = JMXSC;
        this.controle = controle;

    }

    public ThreadMBeanMemoria(ObjectName controle, MBeanServerConnection JMXSC, JTextArea jtaMemoria) {
        this.JMXSC = JMXSC;
        this.controle = controle;
        this.jtaMemoria = jtaMemoria;
    }

    @Override
    public void run() {
        while (true) {
            try {
                if (controle.getKeyProperty("type").equals("MACHINEWINDOWS")) {
                    try {
                        memLivre = (Long) JMXSC.getAttribute(controle, "MemoriaLivre");
                        memOcupada = (Long) JMXSC.getAttribute(controle, "MemoriaOcupada");
                        jtfMemOcupada.setText(memOcupada != null ? memOcupada.toString() : "");
                        jtfMemLivre.setText(memLivre != null ? memLivre.toString() : "");
                    } catch (MBeanException ex) {
                        Logger.getLogger(ThreadMBeanMemoria.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (AttributeNotFoundException ex) {
                        Logger.getLogger(ThreadMBeanMemoria.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InstanceNotFoundException ex) {
                        Logger.getLogger(ThreadMBeanMemoria.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ReflectionException ex) {
                        Logger.getLogger(ThreadMBeanMemoria.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IOException ex) {
                        Logger.getLogger(ThreadMBeanMemoria.class.getName()).log(Level.SEVERE, null, ex);
                    }

                } else {
                    if (controle.getKeyProperty("type").equals("MACHINELINUX")) {
                        try {
                            String memoria = (String) JMXSC.getAttribute(controle, "Memoria");
                            this.jtaMemoria.setText(memoria);
                        } catch (MBeanException ex) {
                            Logger.getLogger(ThreadMBeanMemoria.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (AttributeNotFoundException ex) {
                            Logger.getLogger(ThreadMBeanMemoria.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (InstanceNotFoundException ex) {
                            Logger.getLogger(ThreadMBeanMemoria.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (ReflectionException ex) {
                            Logger.getLogger(ThreadMBeanMemoria.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IOException ex) {
                            Logger.getLogger(ThreadMBeanMemoria.class.getName()).log(Level.SEVERE, null, ex);
                        }

                    }
                }
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException ex) {
                Logger.getLogger(ThreadMBeanMemoria.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
